package net.joint_painting.client.controller;

import gwt.g2d.client.graphics.Color;
import gwt.g2d.client.math.Vector2;
import net.joint_painting.client.model.Model;
import net.joint_painting.client.observer.ViewObject;
import net.joint_painting.client.observer.ViewObserver;
import net.joint_painting.client.observer.ViewObserverListener;
import net.joint_painting.shared.model.Line;

/**
 * @author CAM
 * @version $Revision: 1.0 $
 */
public class ViewController implements ViewObserver {

    private Model model;

    private ViewObserverListener viewObserverListener;

    public ViewController() {
	viewObserverListener = new ViewObserverListener();
	viewObserverListener.attach(this);
    }

    /**
     * Method drawLine.
     * 
     * @param p1
     *            Vector2
     * @param p2
     *            Vector2
     */
    private void drawLine(Vector2 p1, Vector2 p2) {
	Line line = new Line(null, p1.getIntX(), p1.getIntY(), p2.getIntX(),
		p2.getIntY(), model.getPainterSize(), model.getPainterColor());
	model.drawLine(line);
    }

    /**
     * Method getViewObserverListener.
     * 
     * @return ViewObserverListener
     */
    public ViewObserverListener getViewObserverListener() {
	return viewObserverListener;
    }

    /**
     * Method setModel.
     * 
     * @param model
     *            Model
     */
    public void setModel(Model model) {
	this.model = model;
    }

    /**
     * Method update.
     * 
     * @param object
     *            ViewObject
     * 
     * @see net.joint_painting.client.observer.ViewObserver#update(ViewObject)
     */
    @Override
    public void update(ViewObject object) {
	switch (object.getType()) {
	case ZOOMIN:
	    model.zoomIn();
	    break;
	case ZOOMOUT:
	    model.zoomOut();
	    break;
	case MOVEUP:
	    model.moveUp();
	    break;
	case MOVEDOWN:
	    model.moveDown();
	    break;
	case MOVELEFT:
	    model.moveLeft();
	    break;
	case MOVERIGHT:
	    model.moveRight();
	    break;
	case DRAWLINE:
	    drawLine((Vector2) object.getArgument1(),
		    (Vector2) object.getArgument2());
	    break;
	case SETPAINTERSIZE:
	    model.setPainterSize((Integer) object.getArgument1());
	    break;
	case SETPAINTERCOLOR:
	    model.setPainterColor((Color) object.getArgument1());
	    break;
	default:
	    break;
	}
    }

}
